package cn.witsky.rtp2ws.util;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import cn.witsky.rtp2ws.domain.Rtp;

public class LogUtil {
	private static Logger logger = LoggerFactory.getLogger(LogUtil.class);
	private static Cache<String, String> cache = CacheBuilder.newBuilder().expireAfterWrite(5,TimeUnit.SECONDS).build();
	static boolean canPrint(Rtp rtp,String key) {
		String cacheKey="";
		if(rtp!=null) {
			cacheKey=rtp.getRtpId()+":"+key;
		}else {
			cacheKey=key;
		}
		try {
			String val=cache.getIfPresent(cacheKey);
			if(val==null) {
				cache.put(cacheKey, cacheKey);
				return true;
			}else {
				return false;
			}
		}catch(Exception ex) {
			return true;
		}
	}
	public static void info(Rtp rtp,String key,String format,Object... arguments) {
		if(canPrint(rtp,key)) {
			logger.info(key+format, arguments);
		}

	}
	public static void debug(Rtp rtp,String key,String format,Object... arguments) {
		if(canPrint(rtp,key)) {
			logger.debug(key+format, arguments);
		}

	}
	
	public static void warn(Rtp rtp,String key,String format,Object... arguments) {
		if(canPrint(rtp,key)) {
			logger.warn(key+format, arguments);
		}
	}
	public static void error(Rtp rtp,String key,String msg,Exception ex) {
		if(canPrint(rtp,key)) {
			logger.error(key+msg, ex);
		}
	}
	public static void error(Rtp rtp,String key,String msg) {
		if(canPrint(rtp,key)) {
			logger.error(key+msg);
		}
	}
}
